<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="电话号" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="电话号"></el-input>
    </el-form-item>
    <el-form-item label="分机号" prop="extensionNumber">
      <el-input v-model="dataForm.extensionNumber" placeholder="分机号"></el-input>
    </el-form-item>
    <el-form-item label="所属省份id" prop="provinceId">
      <el-input v-model="dataForm.provinceId" placeholder="所属省份id"></el-input>
    </el-form-item>
    <el-form-item label="所属城市id" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="所属城市id"></el-input>
    </el-form-item>
    <el-form-item label="所属区县id" prop="countyId">
      <el-input v-model="dataForm.countyId" placeholder="所属区县id"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="公司名称" prop="companyName">
      <el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
    </el-form-item>
    <el-form-item label="是否默认 0:否，1:是" prop="isDefault">
      <el-input v-model="dataForm.isDefault" placeholder="是否默认 0:否，1:是"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          name: '',
          phoneNumber: '',
          extensionNumber: '',
          provinceId: '',
          cityId: '',
          countyId: '',
          address: '',
          companyName: '',
          isDefault: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '电话号不能为空', trigger: 'blur' }
          ],
          extensionNumber: [
            { required: true, message: '分机号不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '所属省份id不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '所属城市id不能为空', trigger: 'blur' }
          ],
          countyId: [
            { required: true, message: '所属区县id不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          companyName: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' }
          ],
          isDefault: [
            { required: true, message: '是否默认 0:否，1:是不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/users/addressbook/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.addressBook.userId
                this.dataForm.name = data.addressBook.name
                this.dataForm.phoneNumber = data.addressBook.phoneNumber
                this.dataForm.extensionNumber = data.addressBook.extensionNumber
                this.dataForm.provinceId = data.addressBook.provinceId
                this.dataForm.cityId = data.addressBook.cityId
                this.dataForm.countyId = data.addressBook.countyId
                this.dataForm.address = data.addressBook.address
                this.dataForm.companyName = data.addressBook.companyName
                this.dataForm.isDefault = data.addressBook.isDefault
                this.dataForm.createTime = data.addressBook.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/users/addressbook/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'name': this.dataForm.name,
                'phoneNumber': this.dataForm.phoneNumber,
                'extensionNumber': this.dataForm.extensionNumber,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'countyId': this.dataForm.countyId,
                'address': this.dataForm.address,
                'companyName': this.dataForm.companyName,
                'isDefault': this.dataForm.isDefault,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
